package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.AirLane;
import com.example.entity.TakeoffLand;

import com.example.entity.MicroPage;
import com.example.mapper.TakeoffLandMapper;
import com.example.service.interfaces.TakeoffLandInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:51
 **/

@Service
public class TakeoffLandService extends ServiceImpl<TakeoffLandMapper, TakeoffLand> implements TakeoffLandInterface {

    @Autowired
    private TakeoffLandMapper takeoffLandMapper;

    public List<TakeoffLand> findAll(){return takeoffLandMapper.selectList(null);}
    public Optional<TakeoffLand> findByID(Long id){return Optional.ofNullable(takeoffLandMapper.selectById(id));}

    public  TakeoffLand isave(TakeoffLand user){
        takeoffLandMapper.insert(user);
        return user;
    }

    public Optional<TakeoffLand> update(TakeoffLand takeoffLand) {
        int i = takeoffLandMapper.updateById(takeoffLand);
        if(i == 1){
            return Optional.of(takeoffLand);
        }
        return Optional.empty();
    }

    // 返回所有字段数据
    public Optional<TakeoffLand> update2(Integer id, TakeoffLand takeoffLand) {
        TakeoffLand origintakeoffland = takeoffLandMapper.selectById(id);
        if (takeoffLand.getStartStopState() != null) {
            origintakeoffland.setStartStopState(takeoffLand.getStartStopState());
        }
        if (takeoffLand.getCorrespondingTimePeriod() != null) {
            origintakeoffland.setCorrespondingTimePeriod(takeoffLand.getCorrespondingTimePeriod());
        }
        if (takeoffLand.getCapacityThreshold() != null) {
            origintakeoffland.setCapacityThreshold(takeoffLand.getCapacityThreshold());
        }
        if (takeoffLand.getSafeIntervalThreshold() != null) {
            origintakeoffland.setSafeIntervalThreshold(takeoffLand.getSafeIntervalThreshold());
        }
        int i = takeoffLandMapper.updateById(origintakeoffland);
        if(i == 1){
            return Optional.of(origintakeoffland);
        }
        return Optional.empty();
    }


    public boolean delete(Long id) {
        if (takeoffLandMapper.selectById(id) != null) {
            takeoffLandMapper.deleteById(id);
            return true;
        }
        return false;
    }

    public MicroPage<TakeoffLand> page(int current, int size, TakeoffLand condition) {
        QueryWrapper<TakeoffLand> wrapper = new QueryWrapper<>();
        if(condition != null && condition.getAircraftType() != null){
            wrapper.like("application_id", condition.getAircraftType());
        }
        Page<TakeoffLand> page = new Page<>(current, size);
        Page<TakeoffLand> TakeoffLandPage = takeoffLandMapper.selectPage(page, wrapper);
        int total = (int)TakeoffLandPage.getTotal();
        int pageSize = (int)page.getSize();
        MicroPage<TakeoffLand> TakeoffLandMicroPage = new MicroPage<>();
        TakeoffLandMicroPage.setTotal(total);
        TakeoffLandMicroPage.setPageData(TakeoffLandPage.getRecords());
        TakeoffLandMicroPage.setPageNum(current);
        TakeoffLandMicroPage.setPageSize(size);
        Integer pages = (int) Math.ceil((double) total / pageSize);
        TakeoffLandMicroPage.setPages(pages);
        return TakeoffLandMicroPage;
    }
}
